package ch3.ex10;

import static ch3.print.Print.*;
import static ch3.timer.Timer.*;

public class VampireNumber {
	
	static int a(int i) {
		return i/1000;
	}
	static int b(int i) {
		return (i%1000)/100;
	}
	static int c(int i) {
		return ((i%1000)%100)/10;
	}
	static int d(int i) {
		return ((i%1000)%100)%10;
	}
	static int com(int i, int j) {
		return (i * 10) + j;
	}
	static void productTest (int i, int m, int n) {
		if(m * n == i) println(i + " = " + m + " * " + n);
	}	
	
	void getVampireNumbersBasic(int i){
		productTest(i, com(a(i), b(i)), com(c(i), d(i)));
		productTest(i, com(a(i), b(i)), com(d(i), c(i)));
		productTest(i, com(a(i), c(i)), com(b(i), d(i)));
		productTest(i, com(a(i), c(i)), com(d(i), b(i)));
		productTest(i, com(a(i), d(i)), com(b(i), c(i)));
		productTest(i, com(a(i), d(i)), com(c(i), b(i)));
		productTest(i, com(b(i), a(i)), com(c(i), d(i)));
		productTest(i, com(b(i), a(i)), com(d(i), c(i)));
		productTest(i, com(b(i), c(i)), com(d(i), a(i)));
		productTest(i, com(b(i), d(i)), com(c(i), a(i)));
		productTest(i, com(c(i), a(i)), com(d(i), b(i)));
		productTest(i, com(c(i), b(i)), com(d(i), a(i)));
	}
	
	void getVampireNumbers(int i) {
		if (((i + "").length() == 4) && !(i == 1000)) {
			int a = i / 1000;
			int b = (i % 1000) / 100;
			int c = ((i % 1000) % 100) / 10;
			int d = ((i % 1000) % 100) % 10;
			int n, m, nn, mm,nnn,mmm,nnnn,mmmm,mmmmm,nnnnn,nnnnnn,mmmmmm;
			String a_s = "" + a;
			String b_s = "" + b;
			String c_s = "" + c;
			String d_s = "" + d;

			int di = Integer.parseInt(a_s + b_s + c_s + d_s);

			n = Integer.parseInt(a_s + b_s);
			nn = Integer.parseInt(b_s + a_s);
			m = Integer.parseInt(c_s + d_s);
			mm = Integer.parseInt(d_s + c_s);
			
			nnn = Integer.parseInt(a_s + d_s);
			nnnn = Integer.parseInt(d_s + a_s);
			mmm = Integer.parseInt(c_s + b_s);
			mmmm = Integer.parseInt(b_s + c_s);
			
			nnnnn = Integer.parseInt(a_s + c_s);
			nnnnnn = Integer.parseInt(c_s + a_s);
			mmmmm = Integer.parseInt(b_s + d_s);
			mmmmmm = Integer.parseInt(d_s + b_s);
			
			test:	
			for (int r = 0; r < 12; r++) {
				switch (r) {
				case 0:
					if (di == n * m){
						println(di + " = " + n + " * " + m);
					break test;}
				case 1:
					if (di == nn * m){
						println(di + " = " + nn + " * " + m);
					break test;}
				case 2:
					if (di == n * mm){
						println(di + " = " + n + " * " + mm);
					break test;}
				case 3:
					if (di == nn * mm){
						println(di + " = " + nn + " * " + mm);
					break test;}
				case 4:
					if (di == nnn * mmm){
						println(di + " = " + nnn + " * " + mmm);
					break test;}
				case 5:
					if (di == nnnn * mmm){
						println(di + " = " + nnnn + " * " + mmm);
					break test;}
				case 6:
					if (di == nnn * mmmm){
						println(di + " = " + nnn + " * " + mmmm);
					break test;}
				case 7:
					if (di == nnnn * mmmm){
						println(di + " = " + nnnn + " * " + mmmm);
					break test;}
				case 8:
					if (di == nnnnn * mmmmm){
						println(di + " = " + nnnnn + " * " + mmmmm);
					break test;}
				case 9:
					if (di == nnnnnn * mmmmm){
						println(di + " = " + nnnnnn + " * " + mmmmm);
					break test;}
				case 10:
					if (di == nnnnn * mmmmmm){
						println(di + " = " + nnnnn + " * " + mmmmmm);
					break test;}
				case 11:
					if (di == nnnnnn * mmmmmm){
						println(di + " = " + nnnnnn + " * " + mmmmmm);
					break test;}
				}
			}
		}

	}

	public static void main(String[] args) {
		VampireNumber vampireNumber = new VampireNumber();
		long t1 = System.nanoTime();
		for (int i = 0; i < 10000; i++) {
			vampireNumber.getVampireNumbers(i);
		}
		long t2 = System.nanoTime();
		for (int i = 0; i < 10000; i++) {
			if (((i + "").length() == 4) && !(i == 1000)) {
			vampireNumber.getVampireNumbersBasic(i);
			}
		}
		long t3 = System.nanoTime();
		
		timer(t1, t2, t3);
	}
}
